package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.limao.DBDataClass.SysIDEDataClass;
import com.limao.DbConnection.DbConnectionManager;

public class IDEProvider {

	private static class DB_table_ide_ProviderContainer {
		private static IDEProvider instance = new IDEProvider();
	}

	public static IDEProvider getInstance() {
		return DB_table_ide_ProviderContainer.instance;
	}

	// 定义系统表的name与操作语句
	// wawa_table_ide总表查询
	private static final String wawa_table_ide_LOAD = "SELECT * FROM cd_table_ide WHERE table_ide=? ";
	// wawa_table_ide修改资料
	private static final String wawa_table_ide_UPDATE = "UPDATE cd_table_ide SET user_num=? WHERE table_ide=? ";

	// 初始化
	private IDEProvider() {

	}

	// 加载某个表数据
	public SysIDEDataClass Load_table_ide(String table_ide) {
		SysIDEDataClass tmp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement(wawa_table_ide_LOAD);
			pstmt.setString(1, table_ide);

			rs = pstmt.executeQuery();
			if (rs.next()) {
				tmp = new SysIDEDataClass();
				tmp.table_ide = rs.getString(2);
				tmp.table_name = rs.getString(3);
				tmp.user_num = rs.getLong(4);
				tmp.remark = rs.getString(5);
			}

		} catch (Exception e) {
			tmp = null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}

		return tmp;
	}

	// 修改表数据
	public SysIDEDataClass Updata_table_ide(String table_ide, long m) {
		boolean success = false;
		SysIDEDataClass tmp = null;
		SysIDEDataClass tmp2 = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		tmp2 = Load_table_ide(table_ide);
		if (tmp2 != null) {
			try {
				con = DbConnectionManager.getConnection();
				pstmt = con.prepareStatement(wawa_table_ide_UPDATE);
				pstmt.setLong(1, m);
				pstmt.setString(2, table_ide);

				success = pstmt.executeUpdate() == 1;
				if (success) {
					tmp = tmp2;
					tmp.user_num = m;
				}
				tmp2 = null;

			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				DbConnectionManager.closeConnection(rs, pstmt, con);
			}
		}

		return tmp;
	}

}
